<?php

namespace App\Http\Controllers;

use App\Models\Expert;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ExpertController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }
//    public function index2()
//    {
//        return view('expert-people.index2');
//    }
    public function show_expert($id)
    {
        $expert=Expert::find($id);
        $oExperts=Expert::where('id','!=',$expert->id)->take(3)->get();
        return view('front.expert_info')
            ->with('oExperts',$oExperts)
            ->with('oExpert',$expert);
    }
    public function front_expert()
    {
        $oExperts=Expert::paginate(9);
        return view('front.expert')
            ->with('oExperts',$oExperts);
    }


    public function conf()
    {
        $conf=Expert::all();
        return view('front.conf')->with('conf',$conf);
    }
    public function form()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function add()
    {
        return view('admin.expert-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $datas=$request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'ks'=>'required|alpha_dash|min:3|unique:users',
            'zc'=>'required|alpha_dash|min:3|unique:users',
            'yy'=>'required|alpha_dash|min:3|unique:users'
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，-或_，不能包含其他字符',
            'name.min'=>'用户名太短，至少要有:min个字符',
            'name.unique'=>'用户名已存在，不能重复注册',
            'ks.required'=>'ks不能为空',
            'zc.required'=>'zc不能为空',
            'yy.required'=>'yy不能为空'
        ];
        $validate=Validator::make($datas,$rules,$message);
        if ($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        Expert::add($datas);
        return json_encode(['success'=>true,'msg'=>'success']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $experts=Expert::find($id);
        $experts->delete();
        return redirect('/adm/expert');
    }
    public function list_expert()
    {
        $experts=Expert::all();
        return view('admin.expert-adm')->with('experts',$experts);
    }




}
